package com.boot.unity.api.wx.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.alibaba.fastjson.JSONObject;
import com.boot.unity.api.utils.EndeCrypt;
import com.boot.unity.api.utils.IdUtil;
import com.boot.unity.api.utils.images.Base64Utils;
import com.boot.unity.api.wx.dto.PrePayDto;
import com.boot.unity.api.wx.dto.ResponseBean;
import com.boot.unity.api.wx.entity.TrainStu;
import com.boot.unity.api.wx.service.CaptchaCodeManager;
import com.boot.unity.api.wx.service.CommonUtilUrlService;
import com.boot.unity.api.wx.service.WxSignUpService;
import com.boot.unity.api.wx.utils.ResponseUtil;
import com.boot.unity.api.wx.utils.RsaUtils;
import com.sun.istack.internal.NotNull;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 报名
 */
@RestController
@RequestMapping("/wx/signUp")
@Validated
public class WxSignUpController {
    private final Log logger = LogFactory.getLog(WxSignUpController.class);
    public static final String privateKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAzLpjlKaavY4V6z/xjCrmWFHEUqt26ft4uhdDbU2UThUBGDIyS8ssRqFYbvWqg7RmS10FipE2ohEWAkeustbJvQIDAQABAkAg5oNCTFPpyn+zQp9qyyohX2KRdoSxrXwFS9/GHQWMVoIbHyRDKQnDxZmHVzFBPzzgQd+2oh3Kzx26bG1wFbHxAiEA8Wa5v1RDemTWGiD8W37lvF7gWes+8AzRkp+Ft5hjzocCIQDZG+d/dAv/vdXPP3gtE2EiTb6wRuWpicDQD5NlipLSmwIhAMtjYqK6azQHG4DeQ+EO9J8WTO5VzyxPkI95yh7azVSnAiBwAhBt1d324+Zx1KnYf34GZ5uwb/M+g14hOVfu9A4JbwIgTCQjMx0fz5ep+sQZxmOrVwLl7Rd85ey1FqQztdM8wk0=";



    @Autowired
    private WxSignUpService signUpService;

    @Value("${serv.servurl}")
    private String servurl;

    @Autowired
    private CommonUtilUrlService commonUtilUrlService;


    @Value("${img.filePath}")
    private String filePath;

    @Value("${serv.qzip}")
    private String qzip;

    /***
     * 获取公钥
     * @return
     */
    @PostMapping("getPublicKey")
    public Object getPublicKey() {
        return signUpService.getPublicKey();
    }

    /**
     * 报名
     * 信息保存
     *
     * @param
     * @param stu
     * @return
     */
    @PostMapping("save")
    public Object save(@RequestBody TrainStu stu) throws Exception {
        String code = stu.getCode();
        String mobile = EndeCrypt.deCrypt(stu.getMobile());
        JSONObject paramJson = new JSONObject();
        paramJson.put("phoneNumber",mobile);
        paramJson.put("code",code);
        String msgUrl = servurl+"/wx/bindManager/queryMoreNowCode";
        String result = commonUtilUrlService.post(msgUrl, paramJson.toString());
        Thread.sleep(500);
        if(StringUtils.isEmpty(result)){
            return ResponseUtil.fail(703, "验证码错误-1");
        }
        if(!result.equals(code)){
            return ResponseUtil.fail(703, "验证码错误-2");
        }
        String cardNo = EndeCrypt.deCrypt(stu.getCardNo());
        stu.setMobile(mobile);
        stu.setCardNo(cardNo);
        stu.setPhone(mobile);
        return signUpService.save(stu);
    }

    /**
     * 身份证信息识别
     *
     * @param url
     * @return
     * @throws IOException
     */
    @PostMapping("/ocrIdCard")
    public Object ocrIdCard(@RequestBody String url) throws Exception {
//        System.out.println(url);
//        System.out.println("---");

        String imgName = IdUtil.uuid()+".jpg";
        Base64Utils.GenerateImageJPG(url,filePath+"wxfile//",imgName);
        /*System.out.println("请求到了指定路径的图片的base64的数据，并保存成功。");
        //将文件推送到对方的服务器的指定路径下面 yushuaixm
        String qzWxFileUrl = "http://"+qzip+":9002/wx/storage/wx_file_create";
        JSONObject qzParamJson = new JSONObject();
        qzParamJson.put("key",IdUtil.uuid());
        qzParamJson.put("imgStr",url);
        qzParamJson.put("imgName",imgName);
        commonUtilUrlService.post(qzWxFileUrl,qzParamJson.toString());
        System.out.println("请求到了指定路径的图片的base64的数据，发送到了对应的服务器文件。请求地址："+qzWxFileUrl);*/
        String resulturl = url+"wxfile/"+imgName;

        return signUpService.ocrIdCard(resulturl);
    }


    @GetMapping("detail")
    @PostMapping("detail")
    public Object detail(@NotNull String uid, String key) {
        return signUpService.detail(uid, key);
    }


    /**
     * 选班
     * 更新 train_stu驾校、班型信息
     * 正式版本使用
     * @param stu
     * @return
     */
    @PostMapping("update")
    public Object update(@RequestBody TrainStu stu) throws Exception {
        return signUpService.update(stu);
    }

    @PostMapping("prePay")
    public Object prePay(@RequestBody PrePayDto payDto){
        ResponseBean result = signUpService.prePay(payDto);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("action",result.getAction());
        jsonObject.put("body",result.getBody());
        return jsonObject;
    }

    @GetMapping("getStepByUid")
    public Object getStepByUid(String uid,String key) {
        return signUpService.getStepByUid(uid,key);
    }

    /**
     * @param id
     * @return
     */
    @GetMapping("getClassType")
    public Object getClassType(Integer id,String carModel) {
        return signUpService.getClassType(id,carModel);
    }
    /**
     * 获取缴费信息
     * @return
     */
    @GetMapping("getPayParam")
    public Object getPayParam(String uid,String key){
        return signUpService.getPayParam(uid,key);
    }

    /**
     * 获取当前用户的报名进度信息
     * train_stu   train_student
     *
     * @return
     */
    @GetMapping("getStep")
    public Object getStep(String uid,String key) {
        return signUpService.getStep(uid,key);
    }
    /**
     * 获取报名信息
     * @return
     */
    @GetMapping("getStuClassTypeInfo")
    public Object getStuClassTypeInfo(String uid,String key) {
        return signUpService.getStuClassTypeInfo(uid,key);
    }
    /**
     * 保存支付信息
     *
     * @param stuId
     * @return
     */
    @GetMapping("savePayInfo")
    public Object savePayInfo(Integer stuId, String serviceCode,String traceID,String message,String result) {
        return signUpService.savePayInfo(stuId,serviceCode, traceID, message, result);
    }
}
